﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebBanThuoc.Function;
using WebBanThuoc.Server;
using WebBanThuoc.DAO;
using WebBanThuoc.DTO;

namespace WebBanThuoc.admin
{
    public partial class TimKiemDonHang : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!FuncAdmin.IsLogin(Session))
            {
                Session.Add("URLCurrentActive_SendTo_Login", Request.RawUrl);
                Response.Redirect("/admin/DangNhap.aspx");
            }

            if (!IsPostBack)
            {
                getData();
            }
            else
            {
                if (Request.Params["btn_Tim"] != null)
                {
                    getData();
                }
            }
        }

        private void getData()
        {

            IServerWeb sw = new ServerWeb();
            DonHang_DAO dao = new DonHang_DAO(sw.Connection);
            TinhTrangDonHang_DAO daoTT = new TinhTrangDonHang_DAO(sw.Connection);
            KhachHang_DAO daoKH = new KhachHang_DAO(sw.Connection);
            string HoTen = Request.Params["HoTen"] != null ? Request.Params["HoTen"] : "";
            DateTime txt_NgayB = Func.convertDateTimeToString(Request.Params["txt_NgayB"] != null ? Request.Params["txt_NgayB"] : "");
            DateTime txt_NgayE = Func.convertDateTimeToString(Request.Params["txt_NgayE"] != null ? Request.Params["txt_NgayE"] : "");
            string MaTinhTrang = Request.Params["MaTinhTrang"] != null ? Request.Params["MaTinhTrang"] : "";

            List<TinhTrangDonHang> dataTT = daoTT.getListByKey();
            dataTT.Insert(0, new TinhTrangDonHang()
            {
                IsStatus = 0,
                MaTinhTrang = "",
                TenTinhTrang = "---- Tất cả ----"
            });

            List<DonHang> listData = dao.getList_View_DonHang_KhachHangByHoTenBy(HoTen, MaTinhTrang, txt_NgayB, txt_NgayE);
            for (int i = 0; i < listData.Count; i++)
            {
                listData[i].MaTinhTrang = "(" + listData[i].MaTinhTrang + ") " + daoTT.getByKey(listData[i].MaTinhTrang).TenTinhTrang;
                listData[i].TenNguoiNhan = daoKH.getByKey(listData[i].IDKhachHang).HoTen;
            }

            Session.Add("page_TimKiemDH_dataDH", listData);
            Session.Add("page_TimKiemDH_dataTT", dataTT);
            data_GridView.EditIndex = -1;
            Bind_Data();


            sw.closeConnection();

        }

        private void Bind_Data()
        {
            data_GridView.DataSource = Session["page_TimKiemDH_dataDH"];
            data_GridView.DataBind();
        }

        protected void data_GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            data_GridView.PageIndex = e.NewPageIndex;
            Bind_Data();
        }


    }
}